<template>
  <div id="financial">
    <p class="statelist">结算单列表</p>
    <div class="searchtable">
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="供应商名称">
          <el-input
            v-model="formInline.supplierName"
            placeholder="请输入供应商名称"
            clearable
          />
        </el-form-item>
        <el-form-item label="结算状态">
          <el-select v-model="formInline.status" placeholder="请选择">
            <el-option label="待确认" value="0" />
            <el-option label="未结算" value="2" />
            <el-option label="已结算" value="1" />
            <el-option label="全部" value="" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="querysearch()">查询</el-button>
        </el-form-item>
      </el-form>
    </div>

    <div class="table">
      <el-table
        :data="tableData"
        border
        style="width: 100%"
        :cell-style="cellstyle"
        :header-cell-style="rowClass"
        :default-sort="{ prop: 'date', order: 'descending' }"
        :summary-method="getSummaries"
        show-summary
      >
        <el-table-column type="index" width="100" label="序号" />
        <el-table-column prop="supplierName" label="供应商名称" />
        <el-table-column prop="supplierId" label="供应商ID" />
        <el-table-column prop="statementNum" label="订单数量" />
        <el-table-column prop="totalWeight" label="总重量" />
        <el-table-column prop="statementPrice" label="结算金额" sortable />

        <el-table-column prop="statementStatus" label="结算状态">
          <template slot-scope="scope">
            {{ scope.row.statementStatus | statusFitlter }}
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" width="200px">
          <template slot-scope="scope">
            <el-button
              type="primary"
              @click="financialdetail(scope.row.supplierId)"
              >明细</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页数据 -->
    <el-pagination
      @current-change="handleCurrentChange"
      background
      :current-page="pagenum"
      :page-size="pagesize"
      layout="prev,pager,next"
      :total="total - 0"
      style="margin-top:20px;text-align:center"
    ></el-pagination>
  </div>
</template>

<script>
import { getstatementList } from "@/api/account.js";
export default {
  filters: {
    statusFitlter(value) {
      if (value === 0) {
        return "待确认";
      } else if (value === 1) {
        return "已结算";
      } else {
        return "未结算";
      }
    }
  },
  data() {
    return {
      date: "",
      formInline: {
        status: "",
        supplierName: ""
      },
      tableData: [],
      // 显示第几页
      pagenum: 1,
      total: "",
      // 每页显示的数据
      pagesize: 10
    };
  },
  mounted() {
    this.init();
  },
  methods: {
    cellstyle({ row, column, rowIndex, columnIndex }) {
      return "text-align:center";
    },
    rowClass({ row, rowIndex }) {
      return "text-align:center";
    },
    // 合计列
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      var allmoney = 0;
      data.map(item => {
        allmoney += Number(item.statementPrice);
        return allmoney;
      });
      columns.forEach((column, index) => {
        sums[4] = "汇总金额";
        sums[5] = allmoney;
      });

      return sums;
    },
    init() {
       var data = {
        supplierName: this.formInline.supplierName,
        status: this.formInline.status,
        pageNo:this.pagenum
      }
      getstatementList(data)
        .then(res => {
          console.log(res);
          this.tableData = res.rows;
          this.total = res.total
        })
        .catch(err => {
          console.log(err);
        });
    },
    // 查询
    querysearch() {
      this.init();
    },
    // 跳转明细
    financialdetail(id) {
      this.$router.push({
        path: "financialdetail",
        query: { statementId: id }
      });
    },
    // 当前第几页
    handleCurrentChange(val) {
      console.log(val);
      this.pagenum = val;
      this.init();
    }
  }
};
</script>

<style lang="scss" scoped>
#financial {
  padding: 20px 40px;
  .statelist {
    font-size: 22px;
    margin-bottom: 30px;
  }
  .table {
    // width: 800px;
    margin-top: 38px;
  }
}
</style>
